package com.kitty.game.task.service.targetHandler;

import com.kitty.game.role.model.Role;
import com.kitty.game.config.TaskSet;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.confirm.model.ChooseAtkConfirm;
import com.kitty.game.confirm.model.ConfirmType;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.rank.model.monitor.RoleAttribMonitor;
import com.kitty.game.role.message.RespAutoShuxing;
import com.kitty.game.task.model.product.TaskTarget;
import com.kitty.game.task.model.product.TaskTargetType;
import com.kitty.game.task.model.product.targetparam.StudyTargetParam;
import com.kitty.game.task.service.NewTaskService;
import com.kitty.game.team.message.RespConfirm;
import com.kitty.game.utils.Const;
import com.kitty.listener.BaseGameEvent;
import com.kitty.listener.event.ConfirmEvent;
import com.kitty.mina.message.MessagePusher;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class StudyTargetHandler extends TaskTargetHandler {

    @Override
    public void handleEvent(Role role, BaseGameEvent baseGameEvent) {
        ConfirmEvent confirmEvent = (ConfirmEvent)baseGameEvent;
        int taskId = confirmEvent.getTaskId();
        NewTaskService taskService = SpringUtils.getBean(NewTaskService.class);
        TaskSet taskSet = taskService.getTaskSet(taskId, role);
        TaskTarget roleDoingTaskTarget = taskService.getRoleDoingTaskTarget(role, taskSet);

        /**当前任务目标不是学习操作时，返回*/
        if (!taskService.isMatchTargetType(roleDoingTaskTarget, TaskTargetType.STUDY)) {return ;}

        if (confirmEvent.getConfirmType().equals(ConfirmType.CHOOSE_ATK)) {
            SpringUtils.getBean(EquipService.class).addMall("易经洗髓丹",true,role,1);
            /**执行力法选择逻辑*/
            chooseAtk(role, Integer.parseInt(confirmEvent.getSelect()));

            /**更新任务进度*/
            taskService.updateTargetProgress(role, taskSet, roleDoingTaskTarget);
        }
    }

    @Override
    public void triggerNow(Role role, TaskSet taskSet, TaskTarget taskTarget) {
        StudyTargetParam taskTargetParam = (StudyTargetParam) taskTarget.getTaskTargetParam();
        switch (taskTargetParam.getStudyType()) {
            case StudyTargetParam.CHOOSE_ATK:
                popChooseAtk(role, taskSet, taskTarget);
                break;

        }
    }

    /**弹出力法选择*/
    private void popChooseAtk(Role role, TaskSet taskSet, TaskTarget taskTarget) {
        RespConfirm respConfirm = new RespConfirm();
        /**10级力法选择*/
        respConfirm.setTips("ChoseAtkDlg");
        role.setConfirm(new ChooseAtkConfirm(taskSet.getId()));

        MessagePusher.pushMessage(role, respConfirm);
    }

    /**执行任务的力法选择*/
    private void chooseAtk(Role role, int selectType) {
        /**设置方案*/
        if (selectType <1 || selectType > 3) {
            return ;
        }

        Map<String, Byte> nutMap = role.getExtendBox().getAutoAttrib();
        nutMap.put("type", (byte) selectType);
        /**自定义不开启自动洗点*/
        int status = (selectType == Const.ATTRIB_TYPE_SELF ? 0 : 1);
        nutMap.put("status", (byte) status);
        if(selectType==1){
            nutMap.put("con", (byte) 0);
            nutMap.put("str", (byte) 3);
            nutMap.put("dex", (byte) 1);
            nutMap.put("wiz", (byte) 0);
        }else {
            nutMap.put("con", (byte) 0);
            nutMap.put("str", (byte) 0);
            nutMap.put("dex", (byte) 1);
            nutMap.put("wiz", (byte) 3);
        }
        RoleAttribMonitor roleAttribMonitor = new RoleAttribMonitor(role);


        /**免费洗点*/
        role.setStr((short)0);
        role.setWiz((short)0);
        role.setDex((short)0);
        role.setCon((short)0);
        role.setLastAttrib( SpringUtils.getRoleService().getLastAttributePoint(role));

        /**高物伤高法伤给玩家自动加点*/
        SpringUtils.getRoleService().autoAddPoint(role, null);
        role.save();

        roleAttribMonitor.end();

        RespAutoShuxing respAutoShuxing = new RespAutoShuxing();
        respAutoShuxing.setLiliang((byte) nutMap.get("str"));
        respAutoShuxing.setLingli((byte) nutMap.get("wiz"));
        respAutoShuxing.setMinjie((byte) nutMap.get("dex"));
        respAutoShuxing.setTizhi((byte) nutMap.get("con"));
        respAutoShuxing.setStatus((byte) nutMap.get("status"));
        respAutoShuxing.setType((byte) nutMap.get("type"));
        MessagePusher.pushMessage(role, respAutoShuxing);
    }

}
